/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013年8月13日
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.service.system.mapper;

import org.springframework.beans.BeanUtils;

import com.hnas.core.mapper.Mapper;
import com.hnas.sys.orm.Authority;
import com.hnas.sys.orm.AuthorityResource;
import com.hnas.sys.service.system.dto.DTOAuthority;

/**
 * @author 郑兴(zhengxing)
 *
 */
public class AuthorityMapper extends Mapper<DTOAuthority, Authority> {
	private static AuthorityMapper authorityMapper;
	
	private AuthorityMapper() {
		super(DTOAuthority.class, Authority.class);
	}
	
	public static AuthorityMapper getInstance() {
		if (authorityMapper == null) {
			authorityMapper = new AuthorityMapper();
		}
		return authorityMapper;
	}
	
	public DTOAuthority mapToValue(Authority entity) {
		DTOAuthority value = this.mapToValue2(entity);//super.mapToValue(entity);
		
		if(null != entity ) {
			value.setDtoAuthorityResources(AuthorityResourceMapper.getInstance().mapToValues(entity.getAuthorityResources()));
//			StringBuffer sb = new StringBuffer();
			value.setResourceIds(new long[entity.getAuthorityResources().size()]);
			int i=0;
			for (AuthorityResource ar:entity.getAuthorityResources()){
//				if (sb.length()>0)
//					sb.append(",");
//				sb.append(ar.getResource().getId());
				value.getResourceIds()[i++]=ar.getId();
						
			}
//			sb.append(",");
//			value.setResourceIDs(sb.toString());
		}

		return value;
	}
	public DTOAuthority mapToValue2(Authority entity) {
		DTOAuthority value = new DTOAuthority();//super.mapToValue(entity);
		BeanUtils.copyProperties(entity, value, new String[]{"users","authorityResources"});
		return value;
	}
}
